when
is Kotlin's answer to if
/else if
/else
structures.
The simplest use of when
is a bit like a Java switch
statement.
The when
itself gets an expression in parentheses. The contents
of the when
code block consists of values separated from Kotlin statements
or blocks by ->
symbols. Then, the value from the when
expression
is compared against each of those values. If there is a match, the corresponding
statement or block is executed. Using the keyword else
as the value
provides a statement or block to execute if none of the other values matched.
You can learn more about this in:
Tags: